package icu.bughub.app.app.ui.screens

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import icu.bughub.app.app.model.entity.NavigationItem
import icu.bughub.app.app.ui.theme.JetpackComposeBasicTheme

/**
 * @author huangzehui
 * @date 14/5/2024 上午12:16 周二
 * @description
 *
 * https://www.bilibili.com/video/BV1aS4y1D7dv/?p=2&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */
@Composable
fun MainFrame(statusBarHeight:Int) {

    // https://blog.csdn.net/qq_43323894/article/details/135515297

    val navigationItems = listOf(
        NavigationItem(title = "学习", icon = Icons.Filled.Home),
        NavigationItem(title = "任务", icon = Icons.Filled.DateRange),
        NavigationItem(title = "我的", icon = Icons.Filled.Person)
    )

    var currentNavigationIndex by remember {
        mutableIntStateOf(0)
    }

    val selectedColor = Color(0xff149ee7)
    val normalColor = Color(0xff999999)

//    // 旧版本的需要使用这个包裹，新版本的就不需要了
//    ProvideWindowInsets {
//        Scaffold(bottomBar = {}){
//            it.calculateBottomPadding()
//        }
//    }
    Scaffold(bottomBar = {

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .navigationBarsPadding()
                .background(MaterialTheme.colorScheme.surface)
        ) {
            navigationItems.forEachIndexed { index, navigationItem ->


                Column(
                    modifier = Modifier
                        .weight(1f)
                        .height(48.dp)
                        .clickable {
                            currentNavigationIndex = index
                        },
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Image(
                        imageVector = navigationItem.icon,
                        contentDescription = null,
                        colorFilter = ColorFilter.tint(
                            if (currentNavigationIndex == index) selectedColor else normalColor
                        ),
                    )
                    Text(
                        text = navigationItem.title,
                        color = if (currentNavigationIndex == index) selectedColor else normalColor,
                        fontSize = TextUnit(12f, TextUnitType.Sp)
                    )
                }

            }
        }


//        NavigationBar(containerColor = MaterialTheme.colorScheme.surface) {
//            navigationItems.forEachIndexed { index, navigationItem ->
//                NavigationBarItem(
//                    selected = currentNavigationIndex == index,
//                    onClick = {
//                        currentNavigationIndex = index
//                    },
//                    icon = {
//                        Image(
//                            imageVector = navigationItem.icon,
//                            contentDescription = null,
//                            colorFilter = ColorFilter.tint(
//                                if (currentNavigationIndex == index) selectedColor else normalColor
//                            ),
//                        )
//                    },
//                    label = {
//                        Text(text = navigationItem.title)
//                    },
//                    alwaysShowLabel = true,
//                    colors = NavigationBarItemDefaults.colors(
//                        selectedIconColor = selectedColor,
//                        selectedTextColor = selectedColor,
//                        unselectedIconColor = normalColor,
//                        unselectedTextColor = normalColor,
//                        indicatorColor = MaterialTheme.colorScheme.surface
//                    )
//                )
//            }
//        }
    }) {
        Column(modifier = Modifier.padding(bottom = it.calculateBottomPadding())) {
            when (currentNavigationIndex) {
                0 -> {
                    StudyScreen()
                }

                1 -> {
                    TaskScreen()
                }

                2 -> {
                    MineScreen()
                }
            }
        }

    }
}


@Preview
@Composable
fun MainFramePreview() {
    JetpackComposeBasicTheme {
        // A surface container using the 'background' color from the theme
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colorScheme.background
        ) {
            MainFrame(statusBarHeight=30)
        }
    }
}